package com.test.spring4;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * 创建bean的方式
 * @author Romanceling
 *
 */
@Configuration
public class MyConfige {

	@Bean
	public CarFactory createJeepFactory(){
		return new CarFactory();
	}
	
	/**
	 * 直接创建bean
	 * @return
	 */
	@Bean(name = "myBean")//默认方法名createMyBean，也可以指定名字
	@Scope("prototype")//默认是单例singleton，也可以设置成prototype复数
	public MyBean createMyBean(){
		return new MyBean(); 
	}
	
	/**
	 * 工程方法创建bean
	 * @return
	 */
	@Bean
	public RunableFactoryBean createRunableFactoryBean(){
		return new RunableFactoryBean();
	}
	
	@Bean
	public Car createCar(CarFactory factory){
		return factory.create();
	}
	
	@Bean
	public Cat createCat(){
		return new Cat();
	}
	
	//创建和销毁//这种方式最好但是有局限性
	@Bean(initMethod = "init",destroyMethod = "destroy")
	public Dog creatDog(){
		return new Dog();
	}
	
	@Bean
	public Annimal createAnnimal(){
		return new Annimal();
	}
	
}
